package com.hexseek.libretvcloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ExecutorConfig {

    @Bean
    public ExecutorService executorService() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数，即使空闲也会保留
        executor.setCorePoolSize(1);
        // 最大线程数
        executor.setMaxPoolSize(20);
        // 队列容量，当核心线程满时，任务会被放入队列
        executor.setQueueCapacity(100);
        // 线程空闲时间，超过此时间的空闲线程会被销毁
        executor.setKeepAliveSeconds(60);
        // 线程名称前缀
        executor.setThreadNamePrefix("libretv-");
        // 当线程池和队列都满时的拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor.getThreadPoolExecutor();
    }


}
